package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.Car;
import service.ICarService;
import service.impl.CarServiceImpl;
import util.LayData;

@Controller
@RequestMapping("/Car")
public class CarController {

    @Autowired
    private ICarService carService = new CarServiceImpl();

    @PostMapping("/Cars")
    @ResponseBody
    public LayData getAllCars() {
        LayData ld = new LayData();

        ld.setCode(0);
        ld.setMsg("");
        ld.setData(carService.getAllCars());

        return ld;
    }

    @PostMapping("/CarById/{id}")
    @ResponseBody
    public Car getCarById(@PathVariable(value = "id") int id) {
        return carService.getCarById(id);
    }

    @PostMapping("/CarUpdate")
    @ResponseBody
    public LayData updateCar(@RequestBody Car c) {
        LayData ld = new LayData();

        boolean b = carService.updateCar(c);
        if (b) {
            ld.setCode(0);
        }
        return ld;
    }

    @PostMapping("/CarSave")
    @ResponseBody
    public LayData savePerson(@RequestBody Car c) {
        LayData ld = new LayData();

        boolean b = carService.saveCar(c);

        if (b) {
            ld.setCode(0);
        }
        return ld;
    }

    @PostMapping("/CarDelete/{id}")
    public boolean deleteCity(@PathVariable("id") int id){
        return carService.deleteCar(id);
    }
}
